package com.jeff.android.lab.architecture.rxjava2.rxbus;

import android.os.Bundle;
import android.util.Log;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.jeff.utils.ToastUtil;

import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * @author: duff
 * @date: 2019/1/24
 * @since: 1.0.0
 */
public class RxBusFragment extends BaseFragment {

    @Override
    protected int getRootLayout() {
        return R.layout.architecture_fragment_rxbus;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RxBus.getInstance().tObservable(1000, RxBusBaseMessage.class)
                .subscribe(new Consumer<RxBusBaseMessage>() {
                    @Override
                    public void accept(RxBusBaseMessage rxBusBaseMessage) throws Exception {
                        Log.d("jeff", "accept: ");

                        ToastUtil.show(mContext, "accept message ... ");
                    }
                });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        RxBus.getInstance().unRegisterAll();
    }

    @OnClick(R.id.bt_post)
    public void post() {
        RxBus.getInstance().send(1000, new RxBusBaseMessage());
    }

}
